﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Web.Bid.Index" %>

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <%if (Seconds > 0)
      { %>
    <meta http-equiv="refresh" content="<%=Seconds%>" />
    <% } %>
    <meta http-equiv="Content-Language" content="zh-CN" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link type="text/css" href="http://static.silenthink.com/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <%if (ConnectionLimited)
      { %>
    <div class="container">
        亲，您与服务器的连接数已超过了每个用户最大的连接数（<%=ConnectionLimit%>），请关闭不需的浏览器窗口后刷新本页面或</div>
    <% } %>
    <div class="container">
        <div class="row-fluid">
            <ul class="thumbnails">
                <% foreach (var auction in Auctions)
                   { %>
                <li id="item-<%=auction.Id%>" class="span4">
                    <div class="thumbnail">
                        <p>
                            asdf</p>
                        <p class="title">
                            Thumbnail label</p>
                        <img alt="300x200" data-src="holder.js/300x200" style="width: 300px; height: 200px;"
                            src="">
                        <div class="caption">
                            <p class="marketprice">
                                参考价：￥<%=auction.MarketPrice %>
                            </p>
                            <p class="icon">
                            </p>
                            <p id="counter-<%=auction.Id%>" class="counter">
                                <%=auction.Interval %>
                            </p>
                            <p id="price-<%=auction.Id%>" class="price">
                                ￥<%=auction.CurrentPrice.ToString("N")%>
                            </p>
                            <p id="msg-<%=auction.Id%>" class="msg">
                                最高出价<br />
                                <%=auction.CurrentNickName %>
                            </p>
                            <p>
                                <button id="btn-<%=auction.Id%>" class="btn btn-large" type="button" onclick="bid(<%=auction.Id%>)">
                                    出 价</button></p>
                        </div>
                    </div>
                </li>
                <% } %>
            </ul>
        </div>
    </div>
    <input type="text" id="msg" value=" " />
    <input type="button" id="send" value="send" />
    <ul id="message">
    </ul>
    <script type="text/javascript" src="http://static.silenthink.com/js/jquery.min.js"></script>
    <%if (Opened && !ConnectionLimited)
      {%>
    <script type="text/javascript" src="/Scripts/jquery.signalR-1.0.1.min.js"></script>
    <script type="text/javascript" src="/signalr/hubs"></script>
    <script type="text/javascript">
        var userId = <%=Identity.Id %> ;
        var timer = null;
        var isReady = false;
        var marketOpened = <%=Opened.ToString().ToLower() %> ;
        var notifier = $.connection.auctionHub;
        var auctions = <%=JsonData%> ;
        
        function bid(id) {
            if(isReady) {
                var auction = auctions[id];
                if(auction!=null) {
                    if(userId<1) {
                        alert('登录');
                        return;
                    }
                    if (auction.Status==0) {
                        alert('已成交');
                    }
                    if (userId==auction.UserId) {
                        alert('不能连续出价');
                    }
                    else {
                        notifier.server.bid(id)
                            .fail(function (e) {
                                isReady = false;
                            });
                    }
                }
            }else {
                alert('未连接');
            }
        }
        
        function updateAuction(i,now,auction) {
            if (isReady == false ) {
                return;
            }
           else if(auction.UserId==0) {
                return;
            }
            var counter = $("#counter-" + i);
            var price = $("#price-" + i);
            var msg = $("#msg-" + i);
            var btn = $("#btn-" + i);
            
            if (auction.Status == 0) {
                $(counter).html('已成交');
                $(btn).attr('disabled', 'disabled');
                return;
            }
            var interval = auction.Interval - 1;
            if (interval > 0) {
                auction.Interval = interval;
                $(counter).html(interval);
            }
            else {
                auction.Interval = 0;
                $(counter).html('核对出价');
            }
            $(price).html(auction.Price);
            $(msg).html("最高出价<br />"+auction.NickName);
        }

        $(function () {
            $.extend(notifier.client, {
                marketOpened: function () {
                    alert('marketOpened');
                    //window.location.reload();
                },
                marketClosed: function () {
                    isReady = false;
                    $.connection.hub.stop();
                    alert('marketClosed');
                    //window.location.reload();
                },
                priceChanged:function (result) {
                    var id = result.Extra;
                    var now = new Date();
                    var auction = auctions[id];
                    if (result.Status) {
                        var data = result.Data;
                        auction.Price = data.Price;
                        auction.UserId = data.UserId;
                        auction.Interval = data.Interval;
                        auction.NickName = data.NickName;
                        updateAuction(id, now, auction);
                    }
                    else {
                        alert(result.Message);
                        //console.log(result);
                    }
                },
                 statusChanged: function (result) {
                     if (result.Status == true) {
                         var now = new Date();
                         $.each(result.Data, function(i, n) {
                             var auction = auctions[n.Id];
                             auction.Status = 0;
                             updateAuction(i, now, auction);
                         });
                         console.log(auctions);
                     }
                 },disconnected:function () {
                     alert('disconnected');
                 }
            });

          $.connection.hub.error(function() {
                timer = null;
                isReady = false;
                alert("An error occured");
//                setInterval(function() {
//                    if (isReady == false) {
//                    }
//                }, 1000);
            });

            $.connection.hub.start()
                .done(function() {
                    isReady = true;
                    if(marketOpened) {
                        timer = setInterval(function() {
                            var now = new Date();
                            $.each(auctions, function(i, auction) {
                                updateAuction(i, now, auction);
                            });}, 1000);
                    }
                });
        });
        window.onunload = function() {
            if (isReady == true) {
                $.connection.hub.stop(false,true);
            }
        };
    </script>
    <% } %>
</body>
</html>
